define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/order/index' + location.search,
                    add_url: 'shop/order/add',
                    edit_url: 'shop/order/edit',
                    del_url: 'shop/order/del',
                    multi_url: 'shop/order/multi',
                    import_url: 'shop/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");
            var refundStatus ={
                "1":"退款中",
                "2":"退款成功",
                "3":"退款失败",
            };
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'shop.shop_name', title: __('Shop.shop_name'), operate: 'LIKE'},
                        {field: 'order_number', title: __('Order_number'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('Order_user'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: statusList,
                            formatter:function (value,row){
                                if (row.refund_status > 0){
                                    return statusList[value]+'<span style="color: red">(' + refundStatus[row.refund_status] + ')</span>';
                                }else{
                                    return statusList[value];
                                }

                            }
                        },
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'num', title: __('Num')},
                        {field: 'actual_price', title: __('Actual_price'), operate:'BETWEEN'},
                        {field: 'harvest_user', title: __('Harvest_user'), operate: 'LIKE'},
                        {field: 'harvest_phone', title: __('Harvest_phone'), operate: 'LIKE'},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'payment_time', title: __('Payment_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '详情',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'shop/order/detail'
                                },
                                {
                                    name: 'up_status',
                                    text: '取消订单',
                                    icon: 'fa fa-search-plus',
                                    // classname: 'btn btn-info btn-xs btn-addtabs',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'shop/order/up_status',
                                    confirm:'是否取消订单?',
                                    success: function () {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    // success: function (data, ret) {
                                    //     // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                    //     $('.btn-refresh').trigger('click');//
                                    //     //如果需要阻止成功提示，则必须使用return false;
                                    //     //return false;
                                    // },
                                    visible:function (row){
                                        if(row.status == '2' && (row.refund_status == '0' || row.refund_status == '3' )){
                                            return true;
                                        }
                                        return false;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        // up_status: function () {
        //     Fast.api.close();
        //     // Controller.api.bindevent();
        //     // Fast.api.close('1111');
        // },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
